package net.ojbk.study.curriculumDesign.service.cilentservicer.impl;

import net.ojbk.study.curriculumDesign.beans.cilentservicer.ChatSystem;
import net.ojbk.study.curriculumDesign.beans.cilentservicer.Servicer;
import net.ojbk.study.curriculumDesign.beans.usermanage.User;
import net.ojbk.study.curriculumDesign.common.cilentservicer.ServicerStatus;
import net.ojbk.study.curriculumDesign.dao.cilentservicer.ServicerDao;
import net.ojbk.study.curriculumDesign.service.cilentservicer.ICenterManaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: twobox
 * Date: 2019/1/8
 * Time: 16:29
 * Description: No Description
 */
@Service("centerManaService")
public class CenterManaServiceImpl implements ICenterManaService {
    @Autowired
    @Qualifier("servicerDao")
    ServicerDao servicerDao;

    @Autowired
    @Qualifier("chatSystem")
    ChatSystem chatSystem;

    /**
     * 将该用户 成为 该客服中心 的一份子
     *
     * @param user 想成为一份子的人
     */
    @Override
    public Servicer becomeServicer(User user) throws Exception {
        if (user == null || user.getUid() == null || user.getUid().equals(""))
            throw new Exception("新添加的用户存在问题。");
        List<Servicer> servicerList = chatSystem.getServicerList();
        for (Servicer servicer : servicerList) {
            if (servicer.getUid().equals(user.getUid()))
                throw new Exception("已存在该客服");
        }
        //String uid, String name, int status, double divScore
        Servicer newServicer = new Servicer(user.getUid(), user.getUname(), ServicerStatus.STATUS_OK, 100);
        servicerDao.insertOne(newServicer);
        // 我想不能这么使用，因为使得userSession中的servicer对象与集合中对象失去联系
        // 且会失去聊天记录,且会退出失败
        //chatSystem.updateServicerList();
        servicerList.add(newServicer);
        return newServicer;
    }

    /**
     * 这个客服 居然打算从客服中心 逃跑
     *
     * @param servicer 欲删聊天记录跑路的客服
     */
    @Override
    public void escapeCenter(Servicer servicer) throws Exception {
        List<Servicer> servicerList = chatSystem.getServicerList();
        if (servicerList.indexOf(servicer) == -1)
            throw new Exception("此客服不存在");
        servicerDao.deleteOne(servicer);
        servicerList.remove(servicer);
    }

    /**
     * 从数据库中获取所有的客服
     * 静止使用！！！
     *
     * @return
     */
    @Override
    public List<Servicer> getAllServicerByDB() {
        return servicerDao.selectAll();
    }

    /**
     * 获取内存中所有的客服
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Servicer> getAllServicers() {
        return chatSystem.getServicerList();
    }
}
